#include "StringOps.h"

using namespace std;

string& operator - (string& s1, string& s2)
{
	s1.replace(0,s2.length(),s2);
	return s1;
}

string& trim(string& in_rString)
{
    // Trim Both leading and trailing spaces  
    size_t startpos = in_rString.find_first_not_of(" \t"); 
    // Find the first character position after excluding leading blank spaces  
    size_t endpos = in_rString.find_last_not_of(" \t"); 
    // Find the first character position from reverse af  
       
    // if all spaces or empty return an empty string  
    if(( string::npos == startpos ) || ( string::npos == endpos))  
    {  
        in_rString = "";  
    }  
    else
    {
        in_rString = in_rString.substr(startpos, endpos-startpos+1 ); 
    }

    return in_rString;
}

void splitAsNameAndType(string& in_sString, string& in_sName, string& in_sType)
{
    int nLen;
    unsigned int nSplitPos;

    trim(in_sString);
    nSplitPos = in_sString.find_last_of("\t ");
    nLen = in_sString.size();

    if(string::npos == nSplitPos)
        in_sName = in_sString;
    else
    {
        in_sName = in_sString.substr(nSplitPos, nLen -nSplitPos);
        in_sType = in_sString.substr(0,nSplitPos);
    }

    trim(in_sName);
    trim(in_sType);
}

void copyFromFirstInstanceOf(string& in_sString, string& in_sFind, string& out_sResult)
{
    int nLen;
    unsigned int nSplitPos;

    trim(in_sString);
    nSplitPos = in_sString.find(in_sFind);
    nLen = in_sString.size();

    if(string::npos == nSplitPos)
        out_sResult = in_sString;
    else
    {
		int nCopyStartPos = nSplitPos + in_sFind.length();
		out_sResult = in_sString.substr(nCopyStartPos , nLen - nCopyStartPos);
    }

	trim(out_sResult);
}

void copyTillFirstInstanceOf(string& in_sString, string& in_sFind, string& out_sResult)
{
    int nLen;
    unsigned int nSplitPos;

    trim(in_sString);
    nSplitPos = in_sString.find(in_sFind);
    nLen = in_sString.size();

    if(string::npos == nSplitPos)
        out_sResult = in_sString;
    else
    {
		out_sResult = in_sString.substr(0 ,nSplitPos);
    }

	trim(out_sResult);
}

